/**
 * Created by user on 2017/7/10.
 */
$(function () {

    $('img').each(function () {
       imageOperator(this);
    });


    function  imageOperator(ele) {
        var target=document.getElementById(ele);
        var dx=0,dy=0;
        var angle = 0;
        touch.on(ele,'touchstart',function (ev) {
            ev.preventDefault();
        })
        touch.on(ele,'drag',function (ev) {
            var localX=dx+ev.x+'px';
            var localY=dy+ev.y+'px';
            this.style.left=localX;
            this.style.top=localY;
        })
        touch.on(ele,'dragend',function (ev) {
            dx+=ev.x;
            dy+=ev.y;
            this.style.left=dx+'px';
            this.style.top=dy+'px';
        })

//    target.style.webkitTransition = 'all ease 0.05s';

        touch.on(ele, 'pinchstart', function(ev){

            ev.preventDefault();
        });
        var initialScale = 1;
        var currentScale;

        touch.on(ele, 'pinch', function(ev){
            currentScale=ev.scale;
            var reg=new RegExp("scale(.*)+","g");
            //   alert(reg.test(this.style.webkitTransform));
            this.style.webkitTransform = 'scale(' + currentScale + ') '+this.style.webkitTransform.replace(reg,'');
            this.style.msTransform='scale('+currentScale+') '+this.style.msTransform.replace(reg,'') ;
            this.style.mozTransform='scale('+currentScale+') '+this.style.mozTransform.replace(reg,'');
        });

        touch.on(ele, 'pinchend', function(ev){
            initialScale = currentScale;
        });

        touch.on(ele, 'rotate', function(ev){
            var totalAngle = angle + ev.rotation;
            if(ev.fingerStatus === 'end'){
                angle = angle + ev.rotation;
            }
            var reg_Rotate=new RegExp("rotate(.*)+","g");
            //    alert(reg.test(this.style.webkitTransform));
            this.style.webkitTransform= 'rotate(' + totalAngle + 'deg) '+this.style.webkitTransform.replace(reg_Rotate,'');
            this.style.msTransform='rotate(' + totalAngle + 'deg) '+this.style.msTransform.replace(reg_Rotate,'') ;
            this.style.mozTransform='rotate(' + totalAngle + 'deg) '+this.style.mozTransform.replace(reg_Rotate,'');
        });

    }






});
